package com.sls.game.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.http.HttpClient;
import com.sls.core.socket.Head;
import com.sls.core.util.PropertiesUtil;
import com.sls.core.util.ServerDateAndTime;
import com.sls.core.util.UtilMD5Encryption;
import com.sls.game.dao.eightooj.EightZeroJProductDAO;
import com.sls.game.parser.ParseEightZeroJXml;
import com.sls.game.service.EightZeroJProductService;

public class EightZeroJProductServiceImpl implements EightZeroJProductService {

	private static Logger log = Logger
			.getLogger(EightZeroJProductServiceImpl.class);
	private static String fileName = "eightooj.properties";
	private Gson gson;
	@Resource(name = "eightZeroJProductDAO")
	private EightZeroJProductDAO eightZeroJProductDAO;

	public EightZeroJProductServiceImpl() {
		this.gson = CommonUtil.gson();
	}

	@Override
	public void saveSyncProductInfo(String body, Head head) {
		String categoryCode = gson.fromJson(body, String.class);
		String address = PropertiesUtil.getValue(
				"800j.interface.product.list.address", fileName);
		String agent_id = PropertiesUtil.getValue("800j.merchant.id", fileName);
		String md5key = PropertiesUtil.getValue("800j.md5.key", fileName);
		String use_type = "3";
		String time_stamp = ServerDateAndTime.getNowDateTime();
		StringBuilder sb = new StringBuilder();
		sb.append("agent_id=");
		sb.append(agent_id);
		sb.append("&");
		sb.append("use_type=");
		sb.append(use_type);
		sb.append("&");
		sb.append("time_stamp=");
		sb.append(time_stamp);
		sb.append("|||");
		sb.append(md5key);
		String sign = UtilMD5Encryption.createMD5EncryptionOfGB2312(sb
				.toString());
		HttpClient hc = new HttpClient();
		Map<String, String> map = new HashMap<String, String>();
		map.put("agent_id", agent_id);
		map.put("use_type", use_type);
		map.put("time_stamp", time_stamp);
		map.put("sign", sign);
		if (StringUtils.isNotBlank(categoryCode)) {
			map.put("category_code", categoryCode);
		}
		try {
			log.info("请求ID：" + head.getRequestId() + "，访问骏网产品信息地址：" + address
					+ map);
			String xml = hc.doPost(address, map);
			log.info("请求ID：" + head.getRequestId() + "，鑫合骏网产品信息返回：" + xml);
			ParseEightZeroJXml.insertProductInfo(xml, eightZeroJProductDAO);
		} catch (Exception e) {
			log.info("请求ID：" + head.getRequestId() + "，访问骏网产品信息出现异常", e);
		}

	}

	@Override
	public void saveSyncRegionServerInfo(String body, Head head) {
		// TODO Auto-generated method stub

	}

}
